#==============================================
#用于PPV审计 Copyright@Ivan Yan 2023
#2023/10/31更新 增加功效矩阵图分布
#2023/10/09更新 增加PPV不同维度打分的分布情况数据得出和绘图
#2023/10/08更新 增加按技术领域和技术分类wrap
#2023/09/21更新 增加x轴和y轴的核密度直方图组合
#2023/09/20更新 实现关联图和散点图真正融合，并优化箭头显示方式
#2023/09/19更新 增加专利间关联连线,暂时无法直接与ggplot图叠加，需人工在PPT中叠加
#==============================================

library(ggplot2)
library(Cairo)
library(showtext)
library(readxl)
library(dplyr)
library(ggrepel)
library(igraph)
library(ggraph)
library(tidygraph)
library(ggExtra)
library(tidyr)
library(beanplot)
library(tidytext)

font_add("msyh", "msyh.ttf");


Data_raw <- read_excel("专利资产评估PPV打分明细.xlsx", 
                       sheet = "details")

Data_filtered<-Data_raw%>%rename(s1=s1增强竞争地位,s2=s2降低生产成本,
                                 s3=s3改进产品特性,s4=s4提供营销优越性,s5=s5烘托其他产品,
                                 l1=L1经得住法律挑战,l2=L2权利要求范围宽,
                                 l3=L3没有封锁专利,l4=L4专利涵盖关键的竞争要素,l5=L5没有接近的现有技术,
                                 importance=专利重要度,foreign=是否有国外申请,
                                 field=技术领域,tech_cl=技术方向,problem_cl=技术问题分类,law_status=综合法律状态)%>%
  mutate(sum_s=s1*0.4+s2*0.2+s3*0.1+s4*0.2+s5*0.1,
         sum_l=l1*0.4+l2*0.2+l3*0.1+l4*0.2+l5*0.1)%>%
  mutate(sum_t=sum_s+sum_l)%>%
  filter(sum_t>0)%>%
  filter(field %in% c("透明投影","调光材料","调光器件应用","调光器件结构","调光器件工艺","调光产品结构","调光器件驱动"))

#技术类别level的index生成
Data_filtered_num_groupby_tech_cl<-Data_filtered%>%group_by(tech_cl) %>%
  summarise(count = n())
tech_cl_sort<-sort(Data_filtered_num_groupby_tech_cl$count,index.return=TRUE,decreasing=TRUE)
#按技术领域level的index生成
Data_filtered_num_groupby_field<-Data_filtered%>%group_by(field) %>%
  summarise(count = n())
field_sort<-sort(Data_filtered_num_groupby_field$count,index.return=TRUE,decreasing=TRUE)


#参考线数据生成===========
distance<-1
data_up<-data.frame(x=seq(0,5-distance,length.out=20),ymax=5)%>%mutate(ymin=x+distance)#%>%filter(ymax>=ymin)
data_down<-data.frame(x=seq(distance,5,length.out=20),ymin=0)%>%mutate(ymax=x-distance)#%>%filter(ymax>=0)

#核心区区域生成========
core_lower_limit<-3.25
if (core_lower_limit>=5-distance)
{
  core_area<-data.frame(x=seq(5-distance,5,length.out=20),ymax=5,ymin=5-distance)
}else if((core_lower_limit<5-distance)&(core_lower_limit>=5-2*distance)){
  core_area<-rbind(data.frame(x=seq(core_lower_limit,5-distance,length.out=20),ymin=core_lower_limit)%>%mutate(ymax=x+distance),
                   data.frame(x=seq(5-distance,core_lower_limit+distance,length.out=20),ymin=core_lower_limit)%>%mutate(ymax=5),
                   data.frame(x=seq(core_lower_limit+distance,5,length.out=20),ymax=5)%>%mutate(ymin=x-distance))
}else if(core_lower_limit<5-2*distance){
  core_area<-rbind(data.frame(x=seq(core_lower_limit,core_lower_limit+distance,length.out=20),ymin=core_lower_limit)%>%mutate(ymax=x+distance),
                   data.frame(x=seq(core_lower_limit+distance,5-distance,length.out=20))%>%mutate(ymax=x+distance,ymin=x-distance),
                   data.frame(x=seq(5-distance,5,length.out=20),ymax=5)%>%mutate(ymin=x-distance))
}

#p4===效果已优化 叠加法律状态，重要度，有无国外专利的图=====
p4<-ggplot()+
  geom_line(data=data_up,mapping=aes(x=x,y=ymin),size = 0.3)+
  geom_line(data=data_down,mapping=aes(x=x,y=ymax),size = 0.3)+
  geom_ribbon(data=data_up,aes(x=x,ymin=ymin, ymax=ymax),alpha=0.2,fill="black",color=NA)+
  geom_ribbon(data=data_down,aes(x=x,ymin=ymin, ymax=ymax),alpha=0.5,fill="black",color=NA)+
  geom_ribbon(data=core_area,aes(x=x,ymin=ymin, ymax=ymax),alpha=0.2,fill="red",color=NA)+
  geom_point(data=Data_filtered,alpha=0.8,stroke=0.25,colour='black',mapping=aes(x=sum_s,y=sum_l,shape=factor(law_status,level=c("Granted","Pending")),
                                                                                 size=factor(importance,level=c("普通",'重要','核心')),
                                                                                 fill=factor(foreign,level=c("N","PCT","Y","TBD"))))+
  # geom_text(data=Data_filtered,mapping = aes(x=sum_s,y=sum_l, label=申请案卷号))+
  geom_text_repel(data=Data_filtered,mapping = aes(x=sum_s,y=sum_l,label=申请案卷号),
                  size = 1, #注释文本的字体大小
                  box.padding = 0.1, #字到点的距离
                  point.padding = 0.1, #字到点的距离，点周围的空白宽度
                  min.segment.length = 0, #短线段可以省略
                  segment.size=0.25,
                  segment.color = "black")+
  #geom_label(data=Data_filtered,mapping = aes(x=sum_s,y=sum_l,label=申请案卷号),
  #           alpha=0.2,nudge_y = 0.1,size=1,label.size=NA)+
  geom_label(data=data.frame(x=c(4,1),y=c(0.5,4.5),text=c('危险区','浪费区')),mapping = aes(x=x,y=y,label=text),
             alpha=0.5,size=5,colour='white',label.size=NA)+
  scale_shape_manual(values = c(21,22,23),name='法律状态')+
  scale_x_continuous(limits = c(0, 5))+
  scale_fill_manual(values = c("#36BED9","#FBAD01","#FF0000","#CCCCCC"),name='有无国外申请')+
  scale_size_manual(values = c(2,5,10),name='重要性')+
  guides(fill = guide_legend(override.aes=list(shape=21)))+
  coord_fixed(ratio = 1)+
  labs(x="战略重要性",y="法律关系")+
  theme_bw()+
  theme(legend.position = "left")

CairoPDF("plot_all.pdf",width = 8, height =8,family = "msyh")
showtext_begin()
ggMarginal(p4,data=Data_filtered,x=sum_s,y=sum_l,margins ='both',type="densigram")
showtext_end()
dev.off() #关闭图像设备，同时储存图片

#p5===p4 wrap版按技术领域=====
p5<-ggplot()+
  geom_line(data=data_up,mapping=aes(x=x,y=ymin),size = 0.1)+
  geom_line(data=data_down,mapping=aes(x=x,y=ymax),size = 0.1)+
  geom_ribbon(data=data_up,aes(x=x,ymin=ymin, ymax=ymax),alpha=0.2,fill="black",color=NA)+
  geom_ribbon(data=data_down,aes(x=x,ymin=ymin, ymax=ymax),alpha=0.5,fill="black",color=NA)+
  geom_ribbon(data=core_area,aes(x=x,ymin=ymin, ymax=ymax),alpha=0.2,fill="red",color=NA)+
  geom_point(data=Data_filtered,alpha=0.8,stroke=0.1,colour='black',mapping=aes(x=sum_s,y=sum_l,shape=factor(law_status,level=c("Granted","Pending")),
                                                                                size=factor(importance,level=c("普通",'重要','核心')),
                                                                                fill=factor(foreign,level=c("N","PCT","Y","TBD"))))+
  # geom_text(data=Data_filtered,mapping = aes(x=sum_s,y=sum_l, label=申请案卷号))+
  # geom_label(data=Data_filtered,mapping = aes(x=sum_s,y=sum_l,label=申请案卷号),
  #            alpha=0.2,nudge_y = 0.1,size=1,label.size=NA)+
  geom_label(data=data.frame(x=c(4,1),y=c(0.5,4.5),text=c('危险区','浪费区')),mapping = aes(x=x,y=y,label=text),
             alpha=0.5,size=1,colour='white',label.size=NA)+
  geom_text_repel(data=Data_filtered,mapping = aes(x=sum_s,y=sum_l,label=申请案卷号),
                  size = 1, #注释文本的字体大小
                  box.padding = 0, #字到点的距离
                  point.padding = 0, #字到点的距离，点周围的空白宽度
                  min.segment.length = 0, #短线段可以省略
                  segment.size=0.25,
                  segment.color = "black")+
  scale_shape_manual(values = c(21,22,23),name='法律状态')+
  scale_x_continuous(limits = c(0, 5))+
  scale_fill_manual(values = c("#36BED9","#FBAD01","#FF0000","#CCCCCC"),name='有无国外申请')+
  scale_size_manual(values = c(2,4,8),name='重要性')+
  guides(fill = guide_legend(override.aes=list(shape=21)))+
  facet_wrap(~factor(field,levels=Data_filtered_num_groupby_field$field[field_sort$ix]))
  #facet_grid(.~factor(field,levels=Data_filtered_num_groupby_field$field[field_sort$ix]))
#设置X轴和Y轴的缩放比
p5<-p5+coord_fixed(ratio = 1)
#设置x和y轴名称
p5<-p5+labs(x="战略重要性",y="法律关系")
#设置主题
p5<-p5+theme_bw()

CairoPDF("plot_wrap_by_field.pdf",width = 10, height =10,family = "msyh")
showtext_begin()
p5
#ggMarginal(p5,data=Data_filtered,x=sum_s,y=sum_l,margins ='both',type="densigram")
showtext_end()
dev.off() #关闭图像设备，同时储存图片

#p6===p5 中重点领域按技术分类wrap 例如在调光材料重点领域进一步按照技术方向wrap=====
p6<-ggplot()+
  geom_line(data=data_up,mapping=aes(x=x,y=ymin),size = 0.1)+
  geom_line(data=data_down,mapping=aes(x=x,y=ymax),size = 0.1)+
  geom_ribbon(data=data_up,aes(x=x,ymin=ymin, ymax=ymax),alpha=0.2,fill="black",color=NA)+
  geom_ribbon(data=data_down,aes(x=x,ymin=ymin, ymax=ymax),alpha=0.5,fill="black",color=NA)+
  geom_ribbon(data=core_area,aes(x=x,ymin=ymin, ymax=ymax),alpha=0.2,fill="red",color=NA)+
  geom_point(data=filter(Data_filtered,field=='调光材料'),alpha=0.8,stroke=0.1,colour='black',mapping=aes(x=sum_s,y=sum_l,shape=factor(law_status,level=c("Granted","Pending")),
                                                                                size=factor(importance,level=c("普通",'重要','核心')),
                                                                                fill=factor(foreign,level=c("N","PCT","Y","TBD"))))+
  # geom_text(data=Data_filtered,mapping = aes(x=sum_s,y=sum_l, label=申请案卷号))+
  # geom_label(data=Data_filtered,mapping = aes(x=sum_s,y=sum_l,label=申请案卷号),
  #            alpha=0.2,nudge_y = 0.1,size=1,label.size=NA)+
  geom_label(data=data.frame(x=c(4,1),y=c(0.5,4.5),text=c('危险区','浪费区')),mapping = aes(x=x,y=y,label=text),
             alpha=0.5,size=1,colour='white',label.size=NA)+
  geom_text_repel(data=filter(Data_filtered,field=='调光材料'),mapping = aes(x=sum_s,y=sum_l,label=申请案卷号),
                  size = 1, #注释文本的字体大小
                  box.padding = 0, #字到点的距离
                  point.padding = 0, #字到点的距离，点周围的空白宽度
                  min.segment.length = 0, #短线段可以省略
                  segment.size=0.25,
                  segment.color = "black")+
  scale_shape_manual(values = c(21,22,23),name='法律状态')+
  scale_x_continuous(limits = c(0, 5))+
  scale_fill_manual(values = c("#36BED9","#FBAD01","#FF0000","#CCCCCC"),name='有无国外申请')+
  scale_size_manual(values = c(2,4,8),name='重要性')+
  guides(fill = guide_legend(override.aes=list(shape=21)))+
  facet_wrap(~factor(tech_cl))
#设置X轴和Y轴的缩放比
p6<-p6+coord_fixed(ratio = 1)
#设置x和y轴名称
p6<-p6+labs(x="战略重要性",y="法律关系")
#设置主题
p6<-p6+theme_bw()

CairoPDF("plot_wrap_by_techcl.pdf",width = 10, height =10,family = "msyh")
showtext_begin()
p6
#ggMarginal(p5,data=Data_filtered,x=sum_s,y=sum_l,margins ='both',type="densigram")
showtext_end()
dev.off() #关闭图像设备，同时储存图片

#=====================统计技术功效矩阵图：技术问题分类与技术方向的关系==================

#=调光方向
CairoPDF("plot_problem_solution_field_调光.pdf",width = 16, height =8,family = "msyh")
showtext_begin()
Data_filtered%>%
  filter(field %in% c("调光材料","调光器件结构","调光器件工艺","调光产品结构","调光器件驱动"))%>%
  mutate(problem_cl=factor(problem_cl,level=c('调光范围','降低透过态雾度','提高暗态雾度','提高雾态稳定性','色彩优化','降低功耗','改善悬浮性能',
                    '纳米结构均一性','温度对反射波长的影响','工作温宽','提高调光层与基材的粘附力','降低成本','提高良率','任意裁切','安全可靠性',
                    '分区控制','车内应用','坐姿矫正','弱视眼训练','透过率调节','双面投影','提高清晰度','提高背景环境通透率','超短焦','透明投影',
                    '降低透光二次成像','增大视角','背投','紫外阻隔','交互设计','产品外观','维护方便性','产品结构','应用')))%>%
  ggplot(stroke=0.25,colour='black',mapping=aes(label=申请案卷号,x=problem_cl,y=paste(field),shape=factor(law_status,level=c("Granted","Pending")),
                                                                           size=factor(importance,level=c("普通",'重要','核心')),
                                                                           fill=factor(foreign,level=c("N","PCT","Y","TBD"))))+
  geom_point(alpha=0.8,position = position_jitter(width = 0.2, height = 0.2,
                                        seed = 123))+  #geom_jitter(width=0.2,height=0.2)+
  geom_text(position = position_jitter(width = 0.2, height = 0.2,
                                       seed = 123),size=1,vjust=4)+
  scale_shape_manual(values = c(21,22,23),name='法律状态')+
  scale_fill_manual(values = c("#36BED9","#FBAD01","#FF0000","#CCCCCC"),name='有无国外申请')+
  scale_size_manual(values = c(2,4,8),name='重要性')+
  guides(fill = guide_legend(override.aes=list(shape=21)))+
  coord_fixed(ratio = 1)+
  labs(x="技术问题分类",y="技术领域")+
  theme_bw()+
  theme(legend.position = "left",
        axis.text.x = element_text(angle = 30,vjust = 0.85,hjust = 0.75),
        axis.text.y = element_text(hjust=0))

showtext_end()
dev.off() #关闭图像设备，同时储存图片

CairoPDF("plot_problem_solution_field_techcl_调光.pdf",width = 16, height =8,family = "msyh")
showtext_begin()
Data_filtered%>%
  filter(field %in% c("调光材料","调光器件结构","调光器件工艺","调光产品结构","调光器件驱动"))%>%
  mutate(problem_cl=factor(problem_cl,level=c('调光范围','降低透过态雾度','提高暗态雾度','提高雾态稳定性','色彩优化','降低功耗','改善悬浮性能',
                                              '纳米结构均一性','温度对反射波长的影响','工作温宽','提高调光层与基材的粘附力','降低成本','提高良率','任意裁切','安全可靠性',
                                              '分区控制','车内应用','坐姿矫正','弱视眼训练','透过率调节','双面投影','提高清晰度','提高背景环境通透率','超短焦','透明投影',
                                              '降低透光二次成像','增大视角','背投','紫外阻隔','交互设计','产品外观','维护方便性','产品结构','应用')))%>%
  ggplot(stroke=0.25,colour='black',mapping=aes(label=申请案卷号,x=problem_cl,y=paste(field,'-',tech_cl),shape=factor(law_status,level=c("Granted","Pending")),
                                                size=factor(importance,level=c("普通",'重要','核心')),
                                                fill=factor(foreign,level=c("N","PCT","Y","TBD"))))+
  geom_point(alpha=0.8,position = position_jitter(width = 0.2, height = 0.2,
                                                  seed = 123))+  #geom_jitter(width=0.2,height=0.2)+
  geom_text(position = position_jitter(width = 0.2, height = 0.2,
                                       seed = 123),size=1,vjust=4)+
  scale_shape_manual(values = c(21,22,23),name='法律状态')+
  scale_fill_manual(values = c("#36BED9","#FBAD01","#FF0000","#CCCCCC"),name='有无国外申请')+
  scale_size_manual(values = c(2,4,8),name='重要性')+
  guides(fill = guide_legend(override.aes=list(shape=21)))+
  coord_fixed(ratio = 1)+
  labs(x="技术问题分类",y="技术领域")+
  theme_bw()+
  theme(legend.position = "left",
        axis.text.x = element_text(angle = 30,vjust = 0.85,hjust = 0.75),
        axis.text.y = element_text(hjust=0))

showtext_end()
dev.off() #关闭图像设备，同时储存图片

#=透明投影方向

CairoPDF("plot_problem_solution_field_techcl_透明投影.pdf",width = 16, height =8,family = "msyh")
showtext_begin()
Data_filtered%>%
  filter(field %in% c("透明投影"))%>%
  mutate(problem_cl=factor(problem_cl,level=c('调光范围','降低透过态雾度','提高暗态雾度','提高雾态稳定性','色彩优化','降低功耗','改善悬浮性能',
                                              '纳米结构均一性','温度对反射波长的影响','工作温宽','提高调光层与基材的粘附力','降低成本','提高良率','任意裁切','安全可靠性',
                                              '分区控制','车内应用','坐姿矫正','弱视眼训练','透过率调节','双面投影','提高清晰度','提高背景环境通透率','超短焦','透明投影',
                                              '降低透光二次成像','增大视角','背投','紫外阻隔','交互设计','产品外观','维护方便性','产品结构','应用')))%>%
  ggplot(stroke=0.25,colour='black',mapping=aes(label=申请案卷号,x=problem_cl,y=paste(field,'-',tech_cl),shape=factor(law_status,level=c("Granted","Pending")),
                                                size=factor(importance,level=c("普通",'重要','核心')),
                                                fill=factor(foreign,level=c("N","PCT","Y","TBD"))))+
  geom_point(alpha=0.8,position = position_jitter(width = 0.2, height = 0.2,
                                                  seed = 123))+  #geom_jitter(width=0.2,height=0.2)+
  geom_text(position = position_jitter(width = 0.2, height = 0.2,
                                       seed = 123),size=1,vjust=4)+
  scale_shape_manual(values = c(21,22,23),name='法律状态')+
  scale_fill_manual(values = c("#36BED9","#FBAD01","#FF0000","#CCCCCC"),name='有无国外申请')+
  scale_size_manual(values = c(2,4,8),name='重要性')+
  guides(fill = guide_legend(override.aes=list(shape=21)))+
  coord_fixed(ratio = 1)+
  labs(x="技术问题分类",y="技术领域")+
  theme_bw()+
  theme(legend.position = "left",
        axis.text.x = element_text(angle = 30,vjust = 0.85,hjust = 0.75),
        axis.text.y = element_text(hjust=0))

showtext_end()
dev.off() #关闭图像设备，同时储存图片

#预览各个技术领域的技术问题统计排行
CairoPDF("plot_problem_groupby_filed.pdf",width = 16, height =10,family = "msyh")
showtext_begin()
Data_filtered%>%group_by(field,problem_cl)%>%summarise(n=n())%>%
 # ungroup()%>%
  #arrange(field,n)%>%ungroup%>%
  #mutate(problem_cl = reorder(problem_cl, n))%>%
  #ggplot(mapping=aes(x= reorder_within(problem_cl,n,field),y=n,fill=field))+
  ggplot(mapping=aes(x= reorder(problem_cl,n),y=n,fill=field))+
  geom_col()+
  coord_flip()+
  facet_wrap(~field, scales = "free")
showtext_end()
dev.off() #关闭图像设备，同时储存图片
#数据文本数据
Data_filtered%>%group_by(field,problem_cl)%>%summarise(n=n())%>%ungroup()%>%
  arrange(field,-n)%>%
  print( n=50)


#=====统计PPV评分分布云雨图
#按技术领域统计--危险提示：col属性中的排序与legend中的排序需要一致性验证。
CairoPDF("plot_PPV_Distribution_by_field.pdf",width = 10, height =10,family = "msyh")
showtext_begin()

#par(mai=c(0.5,0.5,0.25,2))
data_PPV_by_field<-Data_filtered%>%filter(field!='调光器件驱动')%>%select(战略重要度=sum_s,法律关系=sum_l,field)%>%gather(PPV,value,-field)

beanplot(value ~PPV*field, data_PPV_by_field,col = list("#FF6B5E", "#00C3C2"),
         side = "both",xlab ="技术领域",ylab ="PPV评分")
legend("top",xpd=TRUE,bty="n",c("战略重要度", "法律关系"),
       fill = c("#00C3C2","#FF6B5E"),title="PPV维度")
showtext_end()
dev.off() #关闭图像设备，同时储存图片

#按技术分类统计--危险提示：col属性中的排序与legend中的排序需要一致性验证。
CairoPDF("plot_PPV_Distribution_by_techcl.pdf",width = 10, height =10,family = "msyh")
showtext_begin()

#par(mai=c(0.5,0.5,0.25,2))
data_PPV_by_techcl<-Data_filtered%>%filter(field=='调光材料')%>%select(战略重要度=sum_s,法律关系=sum_l,tech_cl)%>%gather(PPV,value,-tech_cl)

beanplot(value ~PPV*tech_cl, data_PPV_by_techcl,col = list("#FF6B5E", "#00C3C2"),
         side = "both",xlab ="技术分类",ylab ="PPV评分")
legend("top",xpd=TRUE,bty="n",c("战略重要度", "法律关系"),
       fill = c("#00C3C2","#FF6B5E"),title="PPV维度")
showtext_end()
dev.off() #关闭图像设备，同时储存图片



#统计数据文本输出============
print("技术领域-国外申请分布情况统计")
Data_filtered%>%group_by(field,foreign)%>%summarise(n())%>%spread(key=foreign,value='n()')

print("技术领域-PPV评分情况统计")
Data_filtered%>%group_by(field)%>%summarise(mean_PPV=mean(sum_t),mean_law=mean(sum_l),mean_s=mean(sum_s),
                                            sd_PPV=sd(sum_t),max_PPV=max(sum_t),min_PPV=min(sum_t))
print("技术分类-PPV评分情况统计")
Data_filtered%>%group_by(tech_cl)%>%summarise(mean_PPV=mean(sum_t),mean_law=mean(sum_l),mean_s=mean(sum_s),
                                              sd_PPV=sd(sum_t),max_PPV=max(sum_t),min_PPV=min(sum_t))



#======关联图实现===========================
#导入连接数据
connect <- read_excel("专利资产评估PPV打分明细.xlsx", 
                       sheet = "connect")
#追加connect属性，用于后续绘制边属性时使用。
connect<-connect%>%left_join(Data_filtered,by=c('from'='申请案卷号'))
#生成节点数据，基于连接数据=====
#v1基于connect提取节点版
# c(as.character(connect$from),as.character(connect$to)) %>%
#   as_tibble() %>%
#   group_by(value) %>%
#   summarize(n=n()) -> vertices
#v2基于原始数据创建全量节点版
vertices<-Data_filtered%>%mutate(name=factor(申请案卷号,申请案卷号))%>%
                  select(name,sum_s,sum_l,importance,foreign,field,tech_cl,problem,law_status)


#构造graph结构的数据
link_graph<-graph_from_data_frame(connect,vertices=vertices,directed = TRUE)
link_layout_org<-create_layout(link_graph,'stress')
#v1
#link_layout_queue<-link_layout_org%>%left_join(Data_filtered,by=c('name'='申请案卷号'))%>%
                                # mutate(x=sum_s,y=sum_l)%>%
                                # select(x,y,circular,name,n,.ggraph.orig_index,.ggraph.index)
#v2
link_layout_queue<-link_layout_org%>%mutate(x=sum_s,y=sum_l)

#修正布局中节点作为为PPV评估坐标值
link_layout_org$x<-link_layout_queue$x
link_layout_org$y<-link_layout_queue$y
#绘图

#v1==
# p_link<-ggraph(link_layout_org)+
#   geom_edge_link(edge_colour='black',edge_alpha=0.5,edge_width=1)+
#   geom_node_point(color='black',fill='black',alpha=0.5)+
#   #geom_node_text(aes(label=as.character(name))) +
#   scale_x_continuous(limits = c(0, 5))+
#   scale_y_continuous(limits = c(0, 5))+
#   coord_fixed(ratio = 1)
# #  theme_blank()+
# #  theme(panel.background = element_rect(fill = "transparent"))
# MyTheme_transparent <- theme(
#   panel.background = element_rect(fill = "transparent"), # bg of the panel
#   plot.background = element_rect(fill = "transparent", color = NA), # bg of the plot
#   panel.grid.major = element_blank(), # get rid of major grid
#   panel.grid.minor = element_blank(), # get rid of minor grid
#   legend.background = element_rect(fill = "transparent"), # get rid of legend bg
#   legend.box.background = element_rect(fill = "transparent"), # get rid of legend panel bg
#   legend.key = element_rect(fill = "transparent", colour = NA), # get rid of key legend fill, and of the surrounding
#   axis.line = element_line(colour = "black"), # adding a black line for x and y axis
#   panel.border = element_rect(fill=NA,color="black",  linetype="solid")
# )
# p_link<-p_link+MyTheme_transparent

#v2====
p_link<-ggraph(link_layout_org)+
  geom_line(data=data_up,mapping=aes(x=x,y=ymin),size = 0.3)+
  geom_line(data=data_down,mapping=aes(x=x,y=ymax),size = 0.3)+
  geom_ribbon(data=data_up,aes(x=x,ymin=ymin, ymax=ymax),alpha=0.2,fill="black",color=NA)+
  geom_ribbon(data=data_down,aes(x=x,ymin=ymin, ymax=ymax),alpha=0.5,fill="black",color=NA)+
  geom_ribbon(data=core_area,aes(x=x,ymin=ymin, ymax=ymax),alpha=0.2,fill="red",color=NA)+
  geom_point(data=Data_filtered,alpha=0.8,stroke=0.25,colour='black',mapping=aes(x=sum_s,y=sum_l,shape=factor(law_status,level=c("Granted","Pending")),
                                                                                 size=factor(importance,level=c("普通",'重要','核心')),
                                                                                 fill=factor(foreign)))+
  #geom_edge_link(edge_colour='black',edge_alpha=0.5,edge_width=0.5)+
  geom_edge_link(edge_alpha=0.5,edge_width=0.3,
                arrow =arrow(length = unit(1, 'mm')),end_cap = circle(3, 'mm'),
                aes(colour = factor(foreign,level=c('Y','PCT','N'))))+ #edge_colour='black'
  # geom_node_point(alpha=0.8,stroke=0.25,colour='black',mapping=aes(x=sum_s,y=sum_l,shape=factor(law_status,level=c("Granted","Pending")),
  #                             size=factor(importance,level=c("普通",'重要','核心')),
  #                             fill=factor(foreign)))+
  #geom_node_text(aes(label=as.character(name)),size=3,) +
  geom_label(data=data.frame(x=c(4,1),y=c(0.5,4.5),text=c('危险区','浪费区')),mapping = aes(x=x,y=y,label=text),
             alpha=0.5,size=5,colour='white',label.size=NA)+
  geom_text_repel(data=Data_filtered,mapping = aes(x=sum_s,y=sum_l,label=申请案卷号),
                  size = 1, #注释文本的字体大小
                  box.padding = 0.1, #字到点的距离
                  point.padding = 0.1, #字到点的距离，点周围的空白宽度
                  min.segment.length = 0, #短线段可以省略
                  segment.size=0.25,
                  segment.color = "black")+
  
  scale_x_continuous(limits = c(0, 5))+
  scale_y_continuous(limits = c(0, 5))+
  scale_shape_manual(values = c(21,22,23),name='法律状态')+
  scale_fill_manual(values = c("#36BED9","#FBAD01","#FF0000"),name='有无国外申请')+
  scale_size_manual(values = c(2,5,10),name='重要性')+
  guides(fill = guide_legend(override.aes=list(shape=21)))+
  coord_fixed(ratio = 1)+
  labs(x="战略重要性",y="法律关系")+
  theme_bw()

CairoPDF("plot_plink.pdf",width = 8, height =8,family = "msyh")
showtext_begin()
p_link
showtext_end()
dev.off() #关闭图像设备，同时储存图片
# 